﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkSchedueSoft.Data;
using Timer = System.Threading.Timer;

namespace WorkSchedueSoft
{
    public partial class mainForm : Form
    {


        int current_drag_index = 0;

        Timer timer;

        Point start_point;
        bool is_mouse_down;
        bool is_on_drag;

        public mainForm()
        {
            InitializeComponent();

            APPManager.Instance.Main_win = this;

            itemcontextMenuStrip.Items.Add("删除",null, itemcontextMenuStrip_ItemClicked);
            itemcontextMenuStrip.Items.Add("添加",null,addItem);
            itemcontextMenuStrip.Items.Add("编辑",null,editItem);

            contextMenuStrip1.Items.Add("添加", null, addItem);

            timer = new Timer(new TimerCallback((t)=> {
                if (is_mouse_down)
                {
                    Action action = () =>
                    {
                        var point = schedue_listbox.PointToClient(Control.MousePosition);
                        Point pp = new Point(point.X - start_point.X, point.Y - start_point.Y);
                        //double dis = Math.Sqrt(pp.X * pp.X + pp.Y * pp.Y);
                        //Console.WriteLine(pp.Y);
                        if (Math.Abs(pp.Y) > 4)
                        {
                            if (!is_on_drag)
                            {

                                is_on_drag = true;
                                if(schedue_listbox.SelectedItem != null)
                                    this.schedue_listbox.DoDragDrop(this.schedue_listbox.SelectedItem, DragDropEffects.Move);

                            }
                        }
                    };
                    Invoke(action);

                }
            }), null, 1000, 10);

            RefreshData();

        }

        public void RefreshData()
        {
            schedue_listbox.Items.Clear();
            var schedue_items = DataManager.Instance.Schedue_items;
            if (schedue_items.Count > 0)
            {
                foreach (var item in schedue_items)
                {
                    schedue_listbox.Items.Add(item.name);
                }
            }
        }

        private void schedue_listbox_MouseUp(object sender, MouseEventArgs e)
        {
            is_mouse_down = false;
            is_on_drag = false;

            if (e.Button == MouseButtons.Right)
            {
                var point = new Point(e.X, e.Y);
                int posindex = schedue_listbox.IndexFromPoint(point);
                schedue_listbox.ContextMenuStrip = null;
                if (posindex >= 0 && posindex < schedue_listbox.Items.Count)
                {
                    schedue_listbox.SelectedIndex = posindex;
                    itemcontextMenuStrip.Show(schedue_listbox, point);
                }
                else
                {
                    contextMenuStrip1.Show(schedue_listbox, point);
                }
            }
            schedue_listbox.Refresh();
        }

        private void itemcontextMenuStrip_ItemClicked(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除？", "删除", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                var selectedItem = schedue_listbox.SelectedItem;
                if (selectedItem != null)
                {

                    int select_index = schedue_listbox.SelectedIndex;
                    DataManager.Instance.Schedue_items.RemoveAt(select_index);
                    DataManager.Instance.RefreshData();

                    RefreshData();

                }
            }
        }

        private void editItem(object sender, EventArgs e)
        {
            APPManager.Instance.current_editwin_mode = PopWinMode.EDIT;
            int select_index = schedue_listbox.SelectedIndex;
            DataManager.Instance.Current_schedueitem = DataManager.Instance.Schedue_items[select_index];
            EditWin win = new EditWin();
            win.Show();
        }

        private void addItem(object sender, EventArgs e)
        {
            APPManager.Instance.current_editwin_mode = PopWinMode.ADD;
            DataManager.Instance.Current_schedueitem = new SchedueItem() { name = "新添加" };

            EditWin win = new EditWin();
            win.Show();

        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataManager.Instance.SaveData();
        }

        private void schedue_listbox_DoubleClick(object sender, EventArgs e)
        {
            int index = schedue_listbox.SelectedIndex;

            var schedueItem = DataManager.Instance.Schedue_items[index];
            SchedueProcess.Instance.RunSchedue(schedueItem);

        }

        private void Manual(object sender, EventArgs e)
        {
            Process.Start("manual.pdf");
        }

        private void AboutSoft(object sender, EventArgs e)
        {
            About about_win = new About();
            about_win.Show();
        }

        private void schedue_listbox_MouseMove(object sender, MouseEventArgs e)
        {
            int index = schedue_listbox.IndexFromPoint(e.Location);

            // 判断鼠标所在位置是否是有效元素
            if (index != -1 && index < schedue_listbox.Items.Count)
            {

                SchedueItem item = DataManager.Instance.Schedue_items[index];

                if (toolTip.GetToolTip(schedue_listbox) != item.description)
                    toolTip.SetToolTip(schedue_listbox, item.description);

            }
        }

        private void schedue_listbox_KeyDown(object sender, KeyEventArgs e)
        {
            //if(e.KeyCode == Keys.Up)
        }

        private void schedue_listbox_DragDrop(object sender, DragEventArgs e)
        {
            Point point = schedue_listbox.PointToClient(new Point(e.X, e.Y));
            int index = this.schedue_listbox.IndexFromPoint(point);
            if (index < 0)
            {
                index = this.schedue_listbox.Items.Count - 1;
            }
            //获取拖放的数据内容
            object data = e.Data.GetData(typeof(string));
            //删除原数据
            this.schedue_listbox.Items.Remove(data);
            //插入目标数据
            this.schedue_listbox.Items.Insert(index, data);

            //调整datamanager中的schedue列表排列
            SchedueItem oringin_item = DataManager.Instance.Schedue_items[current_drag_index];
            DataManager.Instance.Schedue_items.Remove(oringin_item);
            DataManager.Instance.Schedue_items.Insert(index, oringin_item);
            DataManager.Instance.RefreshData();

            //重置
            is_mouse_down = false;
            is_on_drag = false;

        }

        private void schedue_listbox_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void schedue_listbox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {

                if (this.schedue_listbox.SelectedItem == null)
                {
                    return;
                }
                //开始拖放操作，DragDropEffects为枚举类型。
                //DragDropEffects.Move 为将源数据移动到目标数据
                is_mouse_down = true;
                start_point = e.Location;
                this.current_drag_index = schedue_listbox.SelectedIndex;
            }
        }

    }
}
